package com.groupseven.backend.controller;


import com.groupseven.backend.domain.Log.SysLog;
import com.groupseven.backend.services.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Date;

@ControllerAdvice
public class ExceptionController {
    @Autowired
    SysLogService sysLogService;
//    Logger logger = LoggerFactory.getLogger(ExceptionHandlers.class);
    @ExceptionHandler(value = Exception.class)
    public void handle( Exception e) {
        StackTraceElement stackTraceElement= e.getStackTrace()[0];
        String errorInfo = e +",errorMassage:"+stackTraceElement+","+"errorLine:"+stackTraceElement.getLineNumber();
        SysLog sysLog=new SysLog( new Date(),errorInfo);
        sysLogService.insertSysLog(sysLog); //将异常写入数据库
    }

}

